package com.choudou5.solr.web.handler.sys;

import com.choudou5.solr.annotation.ReqMapping;
import com.choudou5.solr.annotation.ReqMethod;
import com.choudou5.solr.bean.SearchHiBean;
import com.choudou5.solr.bean.common.GroupRespBean;
import com.choudou5.solr.bean.common.PageResult;
import com.choudou5.solr.bean.common.SqlQueryBean;
import com.choudou5.solr.core.util.SolrHelper;
import com.choudou5.solr.framework.constants.ErrorResp;
import com.choudou5.solr.framework.util.PageJumpUtil;
import com.choudou5.solr.framework.util.RequestUtil;
import com.choudou5.solr.service.SearchHiService;
import com.choudou5.solr.service.ServiceHelper;
import com.choudou5.solr.util.LogDeBugUtil;
import com.choudou5.solr.utils.SqlQueryBuilder;
import com.choudou5.solr.web.handler.BaseHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Name：搜索历史处理器
 * @Author：choudou5
 * @Date：2018-07-15
 */
public class SearchHiHandler extends BaseHandler {

    private final SearchHiService searchHiService = ServiceHelper.singleton(SearchHiService.class);

    @Override
    public void execute(String method, HttpServletRequest req, HttpServletResponse resp) {
        logger.info("SearchHiHandler method:{}", method);
        switch (method){
            case "pageList":
                pageList(req, resp);
                break;
            case "stat":
                stat(req, resp);
                break;
            case "optimizeKeyword":
                optimizeKeyword(req, resp);
                break;
            default:
                PageJumpUtil.errorOut(ErrorResp.Not_Found, req, resp);
                break;
        }
    }

    /**
     * 分页列表
     * @param req
     * @param resp
     */
    private void pageList(HttpServletRequest req, HttpServletResponse resp){
        SqlQueryBean sqlQueryBean = SqlQueryBuilder.buildWhereSql(req);
        sqlQueryBean.setOrderDefParam("create_time", "desc");
        PageResult<SearchHiBean> pageResult = searchHiService.findPage(sqlQueryBean);
        req.setAttribute("pageResult", pageResult);
        PageJumpUtil.forward("searchHi", req, resp);
    }

    /**
     * 统计
     * @param req
     * @param resp
     */
    private void stat(HttpServletRequest req, HttpServletResponse resp){
        SqlQueryBean sqlQueryBean = SqlQueryBuilder.buildWhereSql(req);
        PageResult<GroupRespBean> pageResult = searchHiService.stat(sqlQueryBean);
        req.setAttribute("pageResult", pageResult);
        PageJumpUtil.forward("searchStat", req, resp);
    }

    @ReqMapping(desc = "优化关键字", method = ReqMethod.POST)
    private void optimizeKeyword(HttpServletRequest req, HttpServletResponse resp){
        String module = RequestUtil.getStrParameter(req, "module");
        String selectVals = RequestUtil.getStrParameter(req, "selectVals");
        if(!SolrHelper.existCoreName(module) && !module.equals("all")){
            PageJumpUtil.errorOut(ErrorResp.INVALID_PARAM, "无效的模块", req, resp);
            return;
        }
        LogDeBugUtil.debug("selectVals:"+selectVals);
        PageJumpUtil.writeJsonOk("内部暂未实现，敬请期待！", resp);
    }
}
